-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Add lifetime-aware support for Display
impl of Ident
#143185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
This comment has been minimized.
This comment has been minimized.
c9addca
to
c4edd71
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rustbot ready
} | ||
return fmt::Display::fmt(&lifetime, f); | ||
} | ||
|
||
if self.is_raw { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When Ident starts with '
, the contents are printed recursively.
if self.name == kw::StaticLifetime || self.name == kw::UnderscoreLifetime { | ||
false | ||
} else if let Some(lifetime) = self.as_lifetime() { | ||
lifetime.is_raw_guess() | ||
} else { | ||
self.name.can_be_raw() && self.is_reserved() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are two keywords that need to be excluded here.
compiler/rustc_parse/src/errors.rs
Outdated
@@ -2176,7 +2176,7 @@ pub(crate) struct KeywordLifetime { | |||
pub(crate) struct InvalidLabel { | |||
#[primary_span] | |||
pub span: Span, | |||
pub name: Symbol, | |||
pub name: String, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The label needs to be converted to String
, otherwise the error message will call to_ident_string
, which will print 'break
as 'r#break
in the following example.
fn main() {
'break: loop { //~ ERROR invalid label name `'break`
}
}
I haven't checked to see if there are any other label-related errors yet though, there may be similar issues, but it's generally a more borderline case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Oh interesting. 'break
is actually an invalid label but 'r#break
is only a valid label in Rust >=2021) (ignore, not relevant)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, you should be able to use Symbol
instead of String
here (doesn't make much of a difference here tho) (we could actually suggest escaping via r#
(if we have Rust >= 2021) here but that's for another PR)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found the implementation of Symbol
as diagnostic arguments.
rust/compiler/rustc_errors/src/diagnostic_impls.rs
Lines 165 to 169 in f191420
impl IntoDiagArg for Symbol { | |
fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> DiagArgValue { | |
self.to_ident_string().into_diag_arg(path) | |
} | |
} |
It will be transformed to
Ident
, so if I use Symbol
here, It prints invalid label name 'r#break
. I may have to use String
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be a footgun so I'd rather we introduce a newtype that does the label printing behavior - or maybe even further going as to force any symbol to be either newtype wrapped with one indicating a label vs one indicating a lifetime vs non-lifetime non-label. The latter is more churn though
This comment has been minimized.
This comment has been minimized.
lifetime.ident
Display
impl of Ident
d6c180d
to
9306466
Compare
r? compiler |
@@ -3221,7 +3222,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> { | |||
if i != 0 { | |||
binders += ", "; | |||
} | |||
binders += x.as_str(); | |||
let _ = write!(binders, "{x}"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't do this
compiler/rustc_parse/src/errors.rs
Outdated
@@ -2176,7 +2176,7 @@ pub(crate) struct KeywordLifetime { | |||
pub(crate) struct InvalidLabel { | |||
#[primary_span] | |||
pub span: Span, | |||
pub name: Symbol, | |||
pub name: String, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be a footgun so I'd rather we introduce a newtype that does the label printing behavior - or maybe even further going as to force any symbol to be either newtype wrapped with one indicating a label vs one indicating a lifetime vs non-lifetime non-label. The latter is more churn though
@@ -3185,7 +3186,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> { | |||
let mut rm_inner_binders: FxIndexSet<Span> = Default::default(); | |||
let (span, sugg) = if span.is_empty() { | |||
let mut binder_idents: FxIndexSet<Ident> = Default::default(); | |||
binder_idents.insert(Ident::from_str(name.unwrap_or("'a"))); | |||
binder_idents.insert(name.unwrap_or(Ident::from_str("'a"))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not store a symbol? also do we not intern 'a
?
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Signed-off-by: xizheyin <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method comments tell me Symbol
don't keep the rawness flag or edition. As I understand it, Symbol is purely string content, focusing on string de-duplication and efficient comparisons. Ident handles syntax-level information (including raw flags). So I modify Ident
instead of Symbol
.
rust/compiler/rustc_span/src/symbol.rs
Lines 2636 to 2645 in f191420
/// This method is supposed to be used in error messages, so it's expected to be | |
/// identical to printing the original identifier token written in source code | |
/// (`token_to_string`, `Ident::to_string`), except that symbols don't keep the rawness flag | |
/// or edition, so we have to guess the rawness using the global edition. | |
pub fn to_ident_string(self) -> String { | |
// Avoid creating an empty identifier, because that asserts in debug builds. | |
if self == sym::empty { String::new() } else { Ident::with_dummy_span(self).to_string() } | |
} | |
} | |
why not store a symbol? also do we not intern 'a?
So, I choose Ident.
I'd rather we introduce a newtype that does the label printing behavior - or maybe even further going as to force any symbol to be either newtype wrapped with one indicating a label vs one indicating a lifetime vs non-lifetime non-label. The latter is more churn though
Wouldn't this change be a good idea to turn on a separate PR to keep the logic clean.
@@ -3228,7 +3228,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> { | |||
if i != 0 { | |||
binders += ", "; | |||
} | |||
binders += x.as_str(); | |||
binders += &x.to_string(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I change it to &x.to_string()
which call display
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use push_str
} else if let Some(lifetime) = self.as_lifetime() { | ||
lifetime.is_raw_guess() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where do you use this
I use Ident::as_lifetime
here.
do we not intern 'a? |
the logic isn't clean in this PR |
we don't pre-intern |
Signed-off-by: xizheyin <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rustbot ready
@@ -812,6 +812,7 @@ symbols! { | |||
default_field_values, | |||
default_fn, | |||
default_lib_allocator, | |||
default_lifetime: "'a", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I pre-intern 'a
.
/// A newtype around `Symbol` specifically for label printing. | ||
/// This ensures type safety when dealing with labels and provides | ||
/// specialized formatting behavior for label contexts. | ||
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] | ||
pub struct LabelSymbol(Symbol); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I create a new type for label print.
} | ||
binders += x.as_str(); | ||
binders.push_str(&x.to_string()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I use push_str
.
(span, sugg) | ||
}; | ||
|
||
if higher_ranked { | ||
let message = Cow::from(format!( | ||
"consider making the {} lifetime-generic with a new `{}` lifetime", | ||
kind.descr(), | ||
name.unwrap_or("'a"), | ||
name.unwrap_or(Ident::with_dummy_span(sym::default_lifetime)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use interned 'a
Fixes #143150
r? compiler